package com.zr.gdms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.gdms.dao.AppShowDao;
import com.zr.gdms.model.Application;
import com.zr.gdms.model.Teacher;
import com.zr.gdms.util.JDBCUtil;

public class AppShowDaoImpl implements AppShowDao {
   
	@Override
	public int useUidBytid(int uid) {
		int t=0;
	StringBuffer str = new StringBuffer("");
	str.append("SELECT teacher.tid");
	str.append(" FROM teacher");
	str.append(" WHERE uid=?");
    Connection con = JDBCUtil.getConnection();
    PreparedStatement pst;
    try {
		pst = con.prepareStatement(str.toString());
	    pst.setInt(1, uid);
	    ResultSet rs = pst.executeQuery();
	    while (rs.next()) {
	    	 t = rs.getInt("tid");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return t;
	}

	@Override
	public List<Application> allAppByTid(int tid,int page, int rows) {
		List<Application> la = new ArrayList<Application>();
		StringBuffer str = new StringBuffer("");
		str.append("SELECT application.appid,application.apptype,application.appcontent,");
		str.append("application.apptime,application.appstate");
		str.append(" FROM application");
		str.append(" WHERE application.tid=? AND application.apptype='离职申请' ");
		str.append(" LIMIT ?,?");
	    Connection con = JDBCUtil.getConnection();
	    PreparedStatement pst;
	    try {
			pst = con.prepareStatement(str.toString());
		    pst.setInt(1, tid);
		    pst.setInt(2, (page-1)*rows);
			pst.setInt(3, rows);
		    ResultSet rs = pst.executeQuery();
		    while (rs.next()) {
		    	 Application app = new Application();
		    	 app.setAppid(rs.getInt("appid"));
		    	 app.setApptype(rs.getString("apptype"));
		    	 app.setAppcontent(rs.getString("appcontent"));
		    	 app.setApptime(rs.getString("apptime"));
		    	 app.setAppstate(rs.getString("appstate"));
		    	 la.add(app);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return la;
	}

	@Override
	public boolean insertApp(String apptype, String appcontent, String apptime, String appstate,int tid) {
		boolean flag = false;
		StringBuilder str = new StringBuilder("");
		   str.append(" INSERT INTO application(apptype,appcontent,apptime,appstate,tid)");
		   str.append(" VALUES(?,?,?,?,?)");
		   Connection con = JDBCUtil.getConnection();
		   PreparedStatement pst;
		   try {
			pst = con.prepareStatement(str.toString());
			pst.setString(1, apptype);
			pst.setString(2, appcontent);
			pst.setString(3, apptime);
			pst.setString(4, appstate);
			pst.setInt(5, tid);
			int i = pst.executeUpdate();
			if (i>0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
